﻿Imports System.Windows.Shapes
Imports System.Windows.Media

Public Class ReportWindow

    Private _window As Window = Nothing
    Private _panel As StackPanel = Nothing

    Public Sub New(ByVal title As String, _
                   ByVal width As Integer, _
                   ByVal height As Integer)

        Dim Viewer As New ScrollViewer
        Viewer.Margin = New Thickness(12, 12, 12, 12)
        Viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto

        _panel = New StackPanel()

        _window = New Window
        _window.Width = width
        _window.Height = height
        _window.WindowStartupLocation = WindowStartupLocation.CenterScreen
        _window.Title = title
        _window.Tag = Me

        Viewer.Content = _panel
        _window.Content = Viewer

    End Sub

    Public Sub WriteLine(ByVal text As String, Optional ByVal alignment As TextAlignment = TextAlignment.Left)

        Dim Label As New TextBlock
        Label.Text = text
        Label.TextAlignment = alignment
        Label.TextWrapping = TextWrapping.Wrap
        _panel.Children.Add(Label)

    End Sub

    Public Sub WriteLine()

        Dim Label As New TextBlock
        Label.Height = 22
        _panel.Children.Add(Label)

    End Sub

    Public Sub WriteLargeHeading(ByVal text As String)

        WriteLine()
        Dim Label As New TextBlock
        Label.Text = text
        Label.FontWeight = FontWeights.Bold
        Label.FontSize *= 2
        Label.TextWrapping = TextWrapping.Wrap
        _panel.Children.Add(Label)
        WriteHorizontalLine()

    End Sub

    Public Sub WriteSmallHeading(ByVal text As String)

        WriteLine()
        Dim Label As New TextBlock
        Label.Text = text
        Label.FontWeight = FontWeights.Bold
        Label.FontSize *= 1.5
        Label.TextWrapping = TextWrapping.Wrap
        _panel.Children.Add(Label)
        WriteHorizontalLine()

    End Sub

    Private Sub WriteHorizontalLine()

        Dim Line As New Rectangle
        Line.Stroke = Brushes.Black
        _panel.Children.Add(Line)

    End Sub

    Private _table As Grid = Nothing

    Public Sub StartTable(ByVal ParamArray headings() As String)

        ' Create a grid with the required columns
        _table = New Grid
        _table.RowDefinitions.Add(New RowDefinition)
        For I As Integer = 0 To headings.Length - 1
            Dim Column As New ColumnDefinition
            Column.Width = New GridLength(1, GridUnitType.Auto)
            _table.ColumnDefinitions.Add(Column)

            ' Add heading
            Dim Heading As String = headings(I)
            Dim Label As New TextBlock
            Label.Text = Heading
            Label.FontWeight = FontWeights.Bold

            Dim Border As New Border
            Border.Padding = New Thickness(3, 3, 3, 3)
            Border.Child = Label
            _table.Children.Add(Border)

            Grid.SetColumn(Border, I)
            Grid.SetRow(Border, 0)

        Next

    End Sub

    Public Sub WriteTableRow(ByVal ParamArray values() As String)

        ' Perform some initial checks
        If _table Is Nothing Then Throw New Exception("Please call the StartTable method first.")
        If values.Length <> _table.ColumnDefinitions.Count Then Throw New Exception("There must be " + _table.ColumnDefinitions.Count.ToString + " values specified in the data.")

        ' Add the data
        Dim Row As New RowDefinition
        _table.RowDefinitions.Add(Row)

        For I As Integer = 0 To values.Length - 1
            Dim Value As String = values(I)

            Dim Label As New TextBlock
            Label.Text = Value

            Dim Border As New Border
            Border.Padding = New Thickness(3, 3, 3, 3)
            Border.Child = Label

            _table.Children.Add(Border)
            Grid.SetRow(Border, _table.RowDefinitions.Count - 1)
            Grid.SetColumn(Border, I)

        Next

    End Sub

    Public Sub EndTable()
        _panel.Children.Add(_table)
        _table = Nothing
    End Sub

    Public Function ShowDialog() As Boolean?
        Return _window.ShowDialog
    End Function

End Class
